import {login} from "@/api/login/login"
import {setToken} from "@/utils/auth";
import {setAccountName,setAccountId} from '@/utils/account'
const account = {
    state:{
        name:'',
        id:''
    },
    mutations: {
      SET_NAME:(state,name) => {
        state.name = name
      },
      SET_ID:(state,id) => {
        state.id = id
      }
    },
    actions:{
        Login({ commit }, userInfo) {
            let data = {
                username:userInfo.account,
                password:userInfo.password
            }
            return new Promise((resolve,reject) => {
                login(data).then(res => {
                    setToken(res.data.token)
                    let userInfo = JSON.parse(res.data.account);
                    setAccountName(userInfo.username)
                    setAccountId(userInfo.id)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
    }
}
export default account;